package mergeForm

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/mergeForm"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    mergeFormReq "github.com/flipped-aurora/gin-vue-admin/server/model/mergeForm/request"
)

type MergeForm_structService struct {
}

// CreateMergeForm_struct 创建MergeForm_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (MFService *MergeForm_structService) CreateMergeForm_struct(MF *mergeForm.MergeForm_struct) (err error) {
	err = global.GVA_DB.Create(MF).Error
	return err
}

// DeleteMergeForm_struct 删除MergeForm_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (MFService *MergeForm_structService)DeleteMergeForm_struct(MF mergeForm.MergeForm_struct) (err error) {
	err = global.GVA_DB.Delete(&MF).Error
	return err
}

// DeleteMergeForm_structByIds 批量删除MergeForm_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (MFService *MergeForm_structService)DeleteMergeForm_structByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]mergeForm.MergeForm_struct{},"id in ?",ids.Ids).Error
	return err
}

// UpdateMergeForm_struct 更新MergeForm_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (MFService *MergeForm_structService)UpdateMergeForm_struct(MF mergeForm.MergeForm_struct) (err error) {
	err = global.GVA_DB.Save(&MF).Error
	return err
}

// GetMergeForm_struct 根据id获取MergeForm_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (MFService *MergeForm_structService)GetMergeForm_struct(id uint) (MF mergeForm.MergeForm_struct, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&MF).Error
	return
}

// GetMergeForm_structInfoList 分页获取MergeForm_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (MFService *MergeForm_structService)GetMergeForm_structInfoList(info mergeFormReq.MergeForm_structSearch) (list []mergeForm.MergeForm_struct, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&mergeForm.MergeForm_struct{})
    var MFs []mergeForm.MergeForm_struct
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Number != "" {
        db = db.Where("number LIKE ?","%"+ info.Number+"%")
    }
    if info.Troubleshooting_date != nil {
        db = db.Where("troubleshooting_date > ?",info.Troubleshooting_date)
    }
    if info.Manufacturing_serial_number != "" {
        db = db.Where("manufacturing_serial_number LIKE ?","%"+ info.Manufacturing_serial_number+"%")
    }
    if info.Process_design_document_number != "" {
        db = db.Where("process_design_document_number LIKE ?","%"+ info.Process_design_document_number+"%")
    }
    if info.Process_design_document_name != "" {
        db = db.Where("process_design_document_name LIKE ?","%"+ info.Process_design_document_name+"%")
    }
    if info.Responsible_craftman != "" {
        db = db.Where("responsible_craftman = ?",info.Responsible_craftman)
    }
    if info.Responsible_craftman_number != "" {
        db = db.Where("responsible_craftman_number LIKE ?","%"+ info.Responsible_craftman_number+"%")
    }
    if info.Fault_project_file != "" {
        db = db.Where("fault_project_file LIKE ?","%"+ info.Fault_project_file+"%")
    }
    if info.Ata_chapter_level1 != "" {
        db = db.Where("ata_chapter_level1 LIKE ?","%"+ info.Ata_chapter_level1+"%")
    }
    if info.Ata_chapter_level2 != "" {
        db = db.Where("ata_chapter_level2 LIKE ?","%"+ info.Ata_chapter_level2+"%")
    }
    if info.Ata_chapter_name != "" {
        db = db.Where("ata_chapter_name LIKE ?","%"+ info.Ata_chapter_name+"%")
    }
    if info.Operation_and_response_to_design_requirements != "" {
        db = db.Where("operation_and_response_to_design_requirements LIKE ?","%"+ info.Operation_and_response_to_design_requirements+"%")
    }
    if info.Fault_name != "" {
        db = db.Where("fault_name LIKE ?","%"+ info.Fault_name+"%")
    }
    if info.Fault_description != "" {
        db = db.Where("fault_description LIKE ?","%"+ info.Fault_description+"%")
    }
    if info.Fault_principle != "" {
        db = db.Where("fault_principle LIKE ?","%"+ info.Fault_principle+"%")
    }
    if info.Troubleshooting_ideas != "" {
        db = db.Where("troubleshooting_ideas LIKE ?","%"+ info.Troubleshooting_ideas+"%")
    }
    if info.DISR_number != "" {
        db = db.Where("d_i_s_r_number LIKE ?","%"+ info.DISR_number+"%")
    }
    if info.Operarion_description1 != "" {
        db = db.Where("operation_description1 LIKE ?","%"+ info.Operarion_description1+"%")
    }
    if info.Operation_description2 != "" {
        db = db.Where("operation_description2 LIKE ?","%"+ info.Operation_description2+"%")
    }
    if info.Operarion_description3 != "" {
        db = db.Where("operation_description3 LIKE ?","%"+ info.Operarion_description3+"%")
    }
    if info.Operarion_description4 != "" {
        db = db.Where("operation_description4 LIKE ?","%"+ info.Operarion_description4+"%")
    }
    if info.Operation_description5 != "" {
        db = db.Where("operation_description5 LIKE ?","%"+ info.Operation_description5+"%")
    }
    if info.Upload_fault_tree != "" {
        db = db.Where("upload_fault_tree LIKE ?","%"+ info.Upload_fault_tree+"%")
    }
    if info.Conclusion != "" {
        db = db.Where("conclusion LIKE ?","%"+ info.Conclusion+"%")
    }
    if info.Complete_date != nil {
        db = db.Where("complete_date > ?",info.Complete_date)
    }
    if info.Corrective_ction != "" {
        db = db.Where("corrective_ction LIKE ?","%"+ info.Corrective_ction+"%")
    }
    if info.Line_fault_operation != "" {
        db = db.Where("line_fault_operation LIKE ?","%"+ info.Line_fault_operation+"%")
    }
    if info.Line_fault_connector_to != "" {
        db = db.Where("line_fault_connector_to LIKE ?","%"+ info.Line_fault_connector_to+"%")
    }
    if info.Line_fault_pinhole_to != "" {
        db = db.Where("line_fault_pinhole_to LIKE ?","%"+ info.Line_fault_pinhole_to+"%")
    }
    if info.Line_fault_result != "" {
        db = db.Where("line_fault_result LIKE ?","%"+ info.Line_fault_result+"%")
    }
    if info.Line_fault_chapter != "" {
        db = db.Where("line_fault_chapter LIKE ?","%"+ info.Line_fault_chapter+"%")
    }
    if info.Pre_factor_number != "" {
        db = db.Where("pre_factor_number LIKE ?","%"+ info.Pre_factor_number+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	err = db.Limit(limit).Offset(offset).Find(&MFs).Error
	return  MFs, total, err
}
